<?php

namespace App;

use App\Http\Services\FileManager;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;


/**
 * App\Classify
 *
 * @property int $id
 * @property int $enterprise_id 所属企业
 * @property string $name 分类名
 * @property string $desc 描述
 * @property int $merchant_id 商家id
 * @property int $pid 父级id
 * @property string $type 分类类型：medicine药品,selling销售
 * @property string $link_url 链接
 * @property int $img_id 图片id
 * @property int $status 状态
 * @property int $sort 排序
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Classify onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify whereDesc($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify whereEnterpriseId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify whereImgId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify whereLinkUrl($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify whereMerchantId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify wherePid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Classify whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Classify withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Classify withoutTrashed()
 * @mixin \Eloquent
 */
class Classify extends Model
{
    protected $table='classify';
    use SoftDeletes;

    protected $guarded=[];
    public static function getBuyingClassifyList($merchant_id){
        $data=self::whereMerchantId($merchant_id)->whereStatus(1)
            ->whereType('selling')
            ->wherePid(0)
            ->get();
        if (count($data) > 0){
            foreach ($data as $item){
                $item->next_classifies = self::getNextClassify($item->id);
                $item->img_url = FileManager::getImgUrl($item->img_id);
            }
        }
        return $data ?? [];
    }

    public static function getNextClassify($pid){
        $data=self::wherePid($pid)->whereStatus(1)->get();
        if (count($data) > 0){
            foreach ($data as $item){
                $item->next_classifies = self::getNextClassify($item->id);
                $item->img_url = FileManager::getImgUrl($item->img_id);
            }
        }
        return $data ?? [];
    }
    protected static function getNextClassifyIds($pid){
        $sIds=self::wherePid($pid)
            ->pluck('id')
            ->toArray();

        $data=$sIds;

        if (count($sIds) > 0){
            foreach ($sIds as $item){
                $temp=self::getNextClassifyIds($item);
                if (count($temp) > 0)
                    array_merge($data,$temp);
            }
        }
        return $data ?? [];
    }

    public static function getAllSonClassifyIds($fatherId){
        //子一级
        $sonId=self::wherePid($fatherId)
            ->pluck('id')
            ->toArray();

        $data=$sonId;

        if (count($sonId) > 0){
            foreach ($sonId as $item){
                //子N级
                $temp=self::getNextClassifyIds($item);
                if (count($temp) > 0)
                    array_merge($data,$temp);
            }
        }
        return $data ?? [];
    }

    public static function getClassifyInfo($classify_id){
        $data=self::whereId($classify_id)->first();
        if ($data){
            $data->next_classifies = self::getNextClassify($classify_id);
            $data->img_url = FileManager::getImgUrl($data->img_id);
            $temp = $data;
            if (count($data->next_classifies) == 0){
                $data->next_classifies = [self::whereId($classify_id)->first()];
//                \Log::notice('getClassifyInfo:in $data->next_classifies == null');
            }
//            \Log::notice('getClassifyInfo:$temp=>'.json_encode($temp).'---$data=>'.json_encode($data));
        }

        return $data ?? [];
    }

    public static function getClassifyName($classify_id){
        $data=self::whereId($classify_id)->first();
        return $data ? $data->name : '';
    }
}
